--[[
CardEliminateAll
消除所有卡片逻辑

]]
local CardUtil =  require("app.util.CardUtil")
local CardEliminateAll = {}--class("CardEliminateAll")



--需找横轴上的可消除卡片
local function findXindexCleanCard(model,yIndex)
	local col =  model.col_ ;
    local row =  model.row_ ;
    
    local cleanCard ={};--返回的重组卡牌
    
	local countCardSpriteId = nil;
	local countNumber = 0;
	
	for j = 1,row,1 do  --x遍历
 		local id = "static:"..j.."_"..yIndex;
 		local obj = model:getObject(id);
 		local cardSpriteId = obj:getCardSpriteId();
 		--比对是否一致
 		if countCardSpriteId == cardSpriteId and
 			cardSpriteId ~= 0
 		then
	      	countNumber = countNumber +1;
	    	if countNumber == 3 then
    			cleanCard = {
    				"static:"..(j-2).."_"..yIndex,
    				"static:"..(j-1).."_"..yIndex,
        			id,
        			
        			
        		};
	        elseif countNumber > 3 then
	        	cleanCard[#cleanCard + 1] = id;
	        end
	    else
	    	countCardSpriteId = cardSpriteId;
			countNumber = 1;
	    end
	    
	    
	    -- 一列到最后位置
	    if j == row then
	      countNumber=0
	      countCardSpriteId = nil
	    end
	end

	return cleanCard
end
--需找纵轴上的可消除卡片
local function findYindexCleanCard(model,xIndex)
	local col =  model.col_ ;
    local row =  model.row_ ;
    
    local cleanCard ={};--返回的重组卡牌
    
	local countCardSpriteId = nil;
	local countNumber = 0;
	
	for i = 1,col,1 do  --y遍历
 		local id = "static:"..xIndex.."_"..i;
 		local obj = model:getObject(id);
 		local cardSpriteId= obj:getCardSpriteId();
 		
 		--比对是否一致
 		if countCardSpriteId == cardSpriteId  and
 			cardSpriteId ~= 0
 		then
	      	countNumber = countNumber +1;
	    	if countNumber == 3 then
    			cleanCard = {
    				"static:"..xIndex.."_"..(i-2),
    				"static:"..xIndex.."_"..(i-1),
        			id,
        			
        			
        		};
	        elseif countNumber > 3 then
	        	cleanCard[#cleanCard + 1] = id;
	        end
	    else
	    	countCardSpriteId = cardSpriteId;
			countNumber = 1;
	    end
	    
	    
	    -- 一列到最后位置
	    if i == col then
	      countNumber=0
	      countCardSpriteId = nil
	    end
	end

	return cleanCard
end







function CardEliminateAll.getEliminateCard(model)
	local col =  model.col_ ;
    local row =  model.row_ ;
	
	
	local allCleanCard = {};
	
	--横轴
	local cleanCard;
	for i = 1,col,1 do  --y遍历
		cleanCard = findXindexCleanCard(model,i);
		if #cleanCard > 0 then 
			table.insertOneArr(allCleanCard,cleanCard)
		end
	end
	
	
	--接着纵的遍历
	for i = 1,row,1 do  --x遍历
		cleanCard = findYindexCleanCard(model,i)
		
		if #cleanCard > 0 then 
			table.insertOneArr(allCleanCard,cleanCard)
			--break;
		end
	end
	
	return allCleanCard;
end







--[[
消除卡片
]]
function CardEliminateAll.eliminateCard(model,cleanCard,onComplete)  
	cleanCard = table.unique(cleanCard) --先除掉相同重复的
	
	local newCleanCard = {};
	for key,col_v in pairs(cleanCard) do 
 		local obj = model:getObject(col_v);
 		local curentCardSpriteId= obj:getCardSpriteId();
 		if curentCardSpriteId ~= 0 then
 			newCleanCard[#newCleanCard + 1] = col_v;
 		end
 	end
	
	
  	if #newCleanCard > 0 then --数量>0
  		local function onCompleteHandle()
  			if onComplete then onComplete(newCleanCard) end
  		end
  		
	     for key,col_v in pairs(newCleanCard) do 
	 		local obj = model:getObject(col_v);
	 		obj:setCardSpriteId(0);
	 		
	 		if key == #cleanCard then
	 			obj:fadeOut(onCompleteHandle);	
	 		else
	 			obj:fadeOut();
	 		end
	 		
	     end
	 else
	 	if onComplete then onComplete() end
     end
     
     
     --记录消除的数量
     model.fightAccount_:addDestoryCards(#newCleanCard);
end







--[[
整理移动卡片
]]
function CardEliminateAll.sortCard(model) 
    local col =  model.col_ ;
    local row =  model.row_ ;
    
    
  
    for xK = 1,row,1 do  --x遍历
    	local index = 1;
    	 for yK = 1,col,1 do  --y遍历
    	 	local curentId = "static:"..xK.."_"..yK;
    	 	local curentObj = model:getObject(curentId);
			local curentCardSpriteId= curentObj:getCardSpriteId();
			if curentCardSpriteId ~= 0 then --遍历到一个cardspriteid不为0的卡片
				if index ~= yK then --当前卡片和 "static:"..xK.."_"..index 不是同一卡片
					local targetId = "static:"..xK.."_"..index;
					local targetCard =  model:getObject(targetId);
					local targetxIndex,targetyIndex = targetCard:getXYIndexById();
					targetCard:changeId(curentId);
 	  				curentObj:changeId(targetId);
 	  				
 	  				local x,y = CardUtil.getCardPositionByIndex(targetxIndex,targetyIndex)
 	  				curentObj:moveTo(x,y);
 	  				
				end
				index = index+1;
			end
    	end
    end
    
    
    
    local leaveCards = {};   
     for xK = 1,row,1 do   --x遍历
    	 for yK = 1,col,1 do  --y遍历
    		local curentId = "static:"..xK.."_"..yK;
    	 	local curentObj = model:getObject(curentId);
			local curentCardSpriteId= curentObj:getCardSpriteId();
			if curentCardSpriteId == 0 then --遍历到一个cardspriteid不为0的卡片
				local xIndex,yIndex = curentObj:getXYIndexById();
  				curentObj:setPosition(curentObj:getPositionX(),display.height + yIndex *CardUtil.height );
			
				leaveCards[#leaveCards + 1] = curentId;
			end
    	
    	end
    end
    
    
    return leaveCards;
end


--[[

      --动画重新摆正
      for j = 1,row,1 do --遍历x值
      
      	local x = (j-1) * CardViewBehavior.width + CardViewBehavior.width/2 + 20;
      	
      	for i = 1,col,1 do --遍历y值
	 	  	
	 	  	--遍历y
	 	  	local id = "static:"..j.."_"..i;
	 	  	local obj = model:getObject(id);
	 	  	local cardSpriteId= obj:getCardSpriteId();
	 	  	
	 	  	if cardSpriteId == 0 then --对象不存在时  
		 	  	for findY = i,col,1 do --没有去后面找
		 	  		local findId = "static:"..j.."_"..findY;
		 	  		local findObj = model:getObject(findId);
	 	  			local findCardSpriteId= findObj:getCardSpriteId();
	 	  	
	 	  			if findCardSpriteId ~= 0 then
	 	  				
	 	  				obj:changeId(findId);
	 	  				findObj:changeId(id);
	 	  				
	 	  				obj:setPosition(x,display.width + findY*10);
	 	  				obj:updateView();
	 	  				findObj:moveTo(obj:getCardPositionById(j,i));
	 	  				
		 	  			break;
		 	  		end
		 	  	end
	 	  	end
	 	  	
	 	  	
	 	  	--再次检验当前的 cardSpriteId 是否存在
	 	  	cardSpriteId= obj:getCardSpriteId();
	 	  	if cardSpriteId == 0 then --对象不存在时  
	 	  		obj:setPosition(x,display.width + i*10);
	 	  		obj:updateView();
	 	  	end
	 	  	
	 	  	
	 	  end
	 	  
 	  end
]]












--[[
随机创建新的牌组
]]
function CardEliminateAll.createRandomCardSprite(model,leaveCards,onComplete)
	local col =  model.col_ ;
    local row =  model.row_ ;
    
    
	 --随机
	for k,v in pairs(leaveCards) do 
		local targetCard = model:getObject(v);
		local newCardSpriteId = math.random(1, 6);
  		targetCard:setCardSpriteId(newCardSpriteId);
  		local xIndex,yIndex = targetCard:getXYIndexById();
  		--targetCard:setPosition(targetCard:getPositionX(),display.height + yIndex *CardUtil.height );
				
  		
  		local x,y = CardUtil.getCardPositionByIndex(xIndex,yIndex)
  		if k == #leaveCards then --最后一个回调
			targetCard:moveTo(x,y,onComplete);
		else
			targetCard:moveTo(x,y);
		end
		targetCard:fadeIn();
	end
	 
	 
	 --[[
	 for i = 1,col,1 do --遍历y值
     	for j = 1,row,1 do --遍历x值
     	
     		--遍历y
	 	  	local id = "static:"..j.."_"..i;
	 	  	local obj = model:getObject(id);
	 	  	local cardSpriteId= obj:getCardSpriteId();
	 	  	
	 	  	if cardSpriteId == 0 then --对象不存在时  
	 	  		local newCardSpriteId = math.random(1, 6);
	 	  		obj:setCardSpriteId(newCardSpriteId);
	 	  		local x,y = obj:getCardPositionById(j,i)
	 	  		obj:moveTo(x,y,i*0.01);
	 	  	end
     	end
     end
     ]]
end





return CardEliminateAll
